package com.imooc.security.ElGamal;


import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.DHParameterSpec;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.InvalidParameterSpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class ImoocElGamal {

    public static final String src = "imooc security rsa";

    public static void main(String[] args) {
        jdkElGamal();
    }


    public static void jdkElGamal() {
        try {
            //公钥加密，私钥解密
            Security.addProvider(new BouncyCastleProvider());

            //1.初始化密钥
            AlgorithmParameterGenerator algorithmParameterGenerator = null;
            algorithmParameterGenerator = AlgorithmParameterGenerator.getInstance("ElGamal");
            algorithmParameterGenerator.init(256);
            AlgorithmParameters algorithmParameters = algorithmParameterGenerator.generateParameters();
            DHParameterSpec dhParameterSpec = (DHParameterSpec) algorithmParameters.getParameterSpec(DHParameterSpec.class);
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("ElGamal");
            keyPairGenerator.initialize(dhParameterSpec, new SecureRandom());
            KeyPair keyPair = keyPairGenerator.generateKeyPair();
            PublicKey elGamalPublicKey = keyPair.getPublic();
            PrivateKey elGamalPrivateKey = keyPair.getPrivate();
            System.out.println("Public Key; " + Base64.encodeBase64String(elGamalPublicKey.getEncoded()));
            System.out.println("Private Key: " + Base64.encodeBase64String(elGamalPrivateKey.getEncoded()));

            //4.公钥加密、私钥解密——加密
            X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(elGamalPublicKey.getEncoded());
            KeyFactory keyFactory=KeyFactory.getInstance("ElGamal");
            PublicKey publicKey=keyFactory.generatePublic(x509EncodedKeySpec);
            Cipher cipher= Cipher.getInstance("ElGamal");
            cipher.init(Cipher.ENCRYPT_MODE,publicKey);
            byte[] result=cipher.doFinal(src.getBytes());
            System.out.println("公钥加密、私钥解密——加密: "+Base64.encodeBase64String(result));

            //5.公钥加密、私钥解密——解密
            PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(elGamalPrivateKey.getEncoded());
            keyFactory = KeyFactory.getInstance("ElGamal");
            PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
            cipher = Cipher.getInstance("ElGamal");
            cipher.init(Cipher.DECRYPT_MODE,privateKey);
            result=cipher.doFinal(result);
            System.out.println("公钥加密、私钥解密——解密: "+new String(result));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
